package com.googlecode.stemwijzer.client.view;

import java.util.HashMap;
import java.util.Set;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.stemwijzer.client.presenter.AnswerQuestionPresenter;
import com.googlecode.stemwijzer.shared.data.QuestionnaireData;
import com.googlecode.stemwijzer.shared.model.IsAnswer;

public class AnswerQuestionView extends Composite implements
		AnswerQuestionPresenter.Display {

	private static AnswerQuestionViewUiBinder uiBinder = GWT
			.create(AnswerQuestionViewUiBinder.class);

	interface AnswerQuestionViewUiBinder extends
			UiBinder<HTMLPanel, AnswerQuestionView> {
	}

	private HashMap<RadioButton, IsAnswer> options;

	@UiField
	Label question;

	@UiField
	RadioButton answer_strongly_disagree;

	@UiField
	RadioButton answer_disagree;

	@UiField
	RadioButton answer_neutral;

	@UiField
	RadioButton answer_agree;

	@UiField
	RadioButton answer_strongly_agree;

	@UiField
	Button previous;

	@UiField
	Button next;

	@SuppressWarnings("serial")
	public AnswerQuestionView() {
		initWidget(uiBinder.createAndBindUi(this));

		options = new HashMap<RadioButton, IsAnswer>() {
			{
				put(answer_strongly_disagree, QuestionnaireData.STRONGLY_DISAGREE);
				put(answer_disagree, QuestionnaireData.DISAGREE);
				put(answer_neutral, QuestionnaireData.NEUTRAL);
				put(answer_agree, QuestionnaireData.AGREE);
				put(answer_strongly_agree, QuestionnaireData.STRONGLY_AGREE);
			}
		};
		addRadioButtonEventHandlers(options.keySet());
	}

	private void addRadioButtonEventHandlers(final Set<RadioButton> radioButtons) {
		for (final RadioButton radioButton : radioButtons) {
			radioButton
					.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

						@Override
						public void onValueChange(
								ValueChangeEvent<Boolean> event) {
							Log
									.debug("onValueChange: "
											+ event.toDebugString());

							resetRadioButtonsStyle(radioButtons);
							toggleRadioButtonStyle(radioButton, event
									.getValue());
						}
					});
		}
	}

	private static void resetRadioButtonsStyle(Set<RadioButton> radioButtons) {
		for (final RadioButton radioButton : radioButtons) {
			toggleRadioButtonStyle(radioButton, false);
		}
	}

	private static void toggleRadioButtonStyle(final RadioButton radioButton,
			final Boolean isChecked) {
		if (isChecked) {
			radioButton.removeStyleName("unchecked");
			radioButton.addStyleName("checked");
		} else {
			radioButton.removeStyleName("checked");
			radioButton.addStyleName("unchecked");
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void startProcessing() {
		next.setEnabled(false);
	}

	@Override
	public void stopProcessing() {
		next.setEnabled(true);
	}

	@Override
	public void showQuestion(String questionText) {
		question.setText(questionText);
	}

	@Override
	public HasClickHandlers getPrevious() {
		return previous;
	}

	@Override
	public HasClickHandlers getNext() {
		return next;
	}

	@Override
	public IsAnswer getAnswer() {
		for (RadioButton option : options.keySet()) {
			if (option.getValue()) {
				return options.get(option);
			}
		}
		return null;
	}

	@Override
	public void clearAnswer() {
		for (RadioButton radioButton : options.keySet()) {
			radioButton.setValue(false);
			toggleRadioButtonStyle(radioButton, false);
		}
	}
}
